package GUILayer;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.util.Vector;
import java.util.Enumeration;
import java.util.List;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.ButtonModel;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JRadioButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.ButtonGroup;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.AbstractButton;
import javax.swing.JFrame;

import GUIadapter.EmployeeModel;
import GUIadapter.CustomerModel;
import GUIadapter.SupplierModel;
import GUIadapter.PanelTableModelAdapter;
import CtrlLayer.*;
import ModelLayer.Customer;
import ModelLayer.Employee;
import ModelLayer.Supplier;

@SuppressWarnings("serial")
public class PanelKartotek extends JPanel implements Updateable
{
	private PersonCtrl PC;
	private List<Customer> customerList;
	private List<Employee> employeeList;
	private List<Supplier> supplierList;
	private PanelTableModelAdapter panelListe;
	private int indexCustomer;
	private int indexEmployee;
	private int indexSupplier;
	private int valg = 1;
			
	private PopupOpretKunde popupOpretKunde;
	private PopupOpretMedarbejder popupOpretMedarbejder;
	private PopupOpretLeverandr popupOpretLeverandr;

	private PopupRedigerKunde popupRedigerKunde;
	private PopupRedigerMedarbejder popupRedigerMedarbejder;
	private PopupRedigerLeverandr popupRedigerLeverandr;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	public PanelKartotek()
	{
		PC = new PersonCtrl();
		popupOpretKunde = new PopupOpretKunde(this);
		popupOpretMedarbejder = new PopupOpretMedarbejder(this);
		popupOpretLeverandr = new PopupOpretLeverandr(this);

		popupRedigerKunde = new PopupRedigerKunde(this);
		popupRedigerMedarbejder = new PopupRedigerMedarbejder(this);
		popupRedigerLeverandr = new PopupRedigerLeverandr(this);


		JRadioButton rdbtnVisKunde = new JRadioButton("vis kunde",true);
		rdbtnVisKunde.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0)
			{
				if ((indexCustomer >= 0) && (indexCustomer < customerList.size()))
					update();
				valg = 1;
			}
		});
		buttonGroup.add(rdbtnVisKunde);

		JRadioButton rdbtnVisLeverandoer = new JRadioButton("vis leverand\u00F8r",false);
		rdbtnVisLeverandoer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if ((indexSupplier >= 0) && (indexSupplier < supplierList.size()))
					update();
				valg = 2;
			}
		});
		buttonGroup.add(rdbtnVisLeverandoer);

		JRadioButton rdbtnVisMedarbejder = new JRadioButton("vis medarbejder",false);
		rdbtnVisMedarbejder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if ((indexEmployee >= 0) && (indexEmployee < employeeList.size()))
					update();
				valg = 3;
			}
		});
		buttonGroup.add(rdbtnVisMedarbejder);
		
		JButton btnRedigerKunde = new JButton("Rediger kunde");
		btnRedigerKunde.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupRedigerKunde.resetKunde();
				popupRedigerKunde.setVisible(true);
			}
		});

		JButton btnOpretKunde = new JButton("Opret kunde");
		btnOpretKunde.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupOpretKunde.clearFields();
				popupOpretKunde.setVisible(true);
			}
		});

		JButton btnRedigerLeverandr = new JButton("Rediger Leverand\u00F8r");
		btnRedigerLeverandr.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				popupRedigerLeverandr.resetLeverandr();
				popupRedigerLeverandr.setVisible(true);
			}
		});

		JButton btnOpretLeverandr = new JButton("Opret Leverand\u00F8r");
		btnOpretLeverandr.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				popupOpretLeverandr.clearFields();
				popupOpretLeverandr.setVisible(true);
			}
		});

		JButton btnRedigerMedarbejder = new JButton("Rediger Medarbejder");
		btnRedigerMedarbejder.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				popupRedigerMedarbejder.resetMedarbejder();
				popupRedigerMedarbejder.setVisible(true);
			}
		});

		JButton btnOpretMedarbejder = new JButton("Opret Medarbejder");
		btnOpretMedarbejder.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				popupOpretMedarbejder.clearFields();
				popupOpretMedarbejder.setVisible(true);
			}
		});
		
		switch(valg)
		{
			case 1: showCustomer(); break;
			case 2: showSupplier(); break;
			case 3: showEmployee(); break;
		}

				
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panelListe, GroupLayout.DEFAULT_SIZE, 534, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(btnOpretKunde, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(btnRedigerKunde, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addComponent(rdbtnVisKunde))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(btnOpretLeverandr, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(rdbtnVisLeverandoer)
								.addComponent(btnRedigerLeverandr, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(rdbtnVisMedarbejder)
								.addComponent(btnRedigerMedarbejder, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(btnOpretMedarbejder, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnRedigerKunde)
						.addComponent(btnRedigerLeverandr)
						.addComponent(btnRedigerMedarbejder))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnOpretKunde)
						.addComponent(btnOpretLeverandr)
						.addComponent(btnOpretMedarbejder))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(rdbtnVisLeverandoer)
						.addComponent(rdbtnVisKunde)
						.addComponent(rdbtnVisMedarbejder))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panelListe, GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
					.addContainerGap())
		);
		setLayout(groupLayout);
	}

	public void showCustomer(){
		customerList = PC.getCustomerList();
		panelListe = new PanelTableModelAdapter(new CustomerModel(customerList));
		panelListe.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Kartotek", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelListe.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panelListe.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
			{
				@Override
				public void valueChanged(ListSelectionEvent event)
				{
					if (event.getValueIsAdjusting() == false)
					{
						indexCustomer = panelListe.getTable().getSelectedRow();
					}
				}
			});
	}
	
	public void showEmployee(){
		employeeList = PC.getEmployeeList();
		panelListe = new PanelTableModelAdapter(new EmployeeModel(employeeList));
		panelListe.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Kartotek", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelListe.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panelListe.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
			{
				@Override
				public void valueChanged(ListSelectionEvent event)
				{
					if (event.getValueIsAdjusting() == false)
					{
						indexEmployee = panelListe.getTable().getSelectedRow();
					}
				}
			});
	}
	
	public void showSupplier(){
		supplierList = PC.getSupplierList();
		panelListe = new PanelTableModelAdapter(new SupplierModel(supplierList));
		panelListe.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Kartotek", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelListe.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panelListe.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
			{
				@Override
				public void valueChanged(ListSelectionEvent event)
				{
					if (event.getValueIsAdjusting() == false)
					{
						indexSupplier = panelListe.getTable().getSelectedRow();
					}
				}
			});
	}
	
	@Override
	public void update(){
		panelListe.getTable().revalidate();
		panelListe.getTable().repaint();
	}
}
